/*
 * bsp_timer.c
 *
 *  Created on: 2023年6月28日
 *      Author: mcealt_DS
 */

#include <bsp_letimer.h>
#include "app_log.h"
#include "em_cmu.h"

void letimerInit(void)
{
  uint32_t count;
  LETIMER_Init_TypeDef letimerInit = LETIMER_INIT_DEFAULT;

  CMU_ClockEnable(cmuClock_LFXO, true);
  CMU_ClockEnable(cmuClock_LETIMER0, true);

    letimerInit.enable = false;
    letimerInit.debugRun = false;
    letimerInit.comp0Top = true;
    letimerInit.bufTop = false;
    letimerInit.out0Pol = 0;
    letimerInit.out1Pol = 0;
    letimerInit.ufoa0 = letimerUFOANone;
    letimerInit.ufoa1 = letimerUFOANone;
    letimerInit.repMode = letimerRepeatFree;
    letimerInit.topValue = 0;

  LETIMER_Init(LETIMER0, &letimerInit);
  count = CMU_ClockFreqGet(cmuClock_LETIMER0) / 1000; //1ms interrupt
  LETIMER_TopSet(LETIMER0, count);
  LETIMER_Enable(LETIMER0, true);
  LETIMER_IntEnable(LETIMER0, TIMER_IF_OF);

    NVIC_EnableIRQ(LETIMER0_IRQn);
}

static uint64_t Tick = 0;
#if 0
void LETIMER0_IRQHandler(void)
{
  uint32_t intFlags = LETIMER_IntGet(LETIMER0);
  LETIMER_IntClear(LETIMER0, intFlags);
  if(intFlags & TIMER_IF_OF){
    Tick++;
  }
}
#endif

uint64_t getLetimerTick(void){
  return Tick;
}
